package br.com.knowledge.persistence.hibernate.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import br.com.knowledge.persistence.hibernate.entities.CadProjeto;

/**
 * A data access object (DAO) providing persistence and search support for
 * CadProjeto entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see br.com.knowledge.persistence.hibernate.entities.CadProjeto
 * @author MyEclipse Persistence Tools
 */

public class CadProjetoDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory .getLogger(CadProjetoDAO.class);
	
	private EntityManager em;    
	
	// property constants
	public static final String NMPROJETO = "nmprojeto";
	public static final String DESCRICAO = "descricao";
	public static final String INDINATIVO = "indinativo";
	public static final String CONFIGSGDB = "configsgdb";
	public static final String NMUSERSGDB = "nmusersgdb";
	public static final String DRIVERSGDB = "driversgdb";

	public void save(CadProjeto transientInstance) {
		log.debug("saving CadProjeto instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CadProjeto persistentInstance) {
		log.debug("deleting CadProjeto instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CadProjeto findById(java.lang.Integer id) {
		log.debug("getting CadProjeto instance with id: " + id);
		try {
			CadProjeto instance = (CadProjeto) getSession()
					.get(
							"br.com.knowledge.persistence.hibernate.entities.CadProjeto",
							id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<CadProjeto> findByExample(CadProjeto instance) {
		log.debug("finding CadProjeto instance by example");
		try {
			List<CadProjeto> results = (List<CadProjeto>) getSession()
					.createCriteria(
							"br.com.knowledge.persistence.hibernate.entities.CadProjeto")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CadProjeto instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CadProjeto as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CadProjeto> findByNmprojeto(Object nmprojeto) {
		return findByProperty(NMPROJETO, nmprojeto);
	}

	public List<CadProjeto> findByDescricao(Object descricao) {
		return findByProperty(DESCRICAO, descricao);
	}

	public List<CadProjeto> findByIndinativo(Object indinativo) {
		return findByProperty(INDINATIVO, indinativo);
	}

	public List<CadProjeto> findByConfigsgdb(Object configsgdb) {
		return findByProperty(CONFIGSGDB, configsgdb);
	}

	public List<CadProjeto> findByNmusersgdb(Object nmusersgdb) {
		return findByProperty(NMUSERSGDB, nmusersgdb);
	}

	public List findAll() {
		log.debug("finding all CadProjeto instances");
		try {
			String queryString = "from CadProjeto";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CadProjeto merge(CadProjeto detachedInstance) {
		log.debug("merging CadProjeto instance");
		try {
			CadProjeto result = (CadProjeto) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CadProjeto instance) {
		log.debug("attaching dirty CadProjeto instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CadProjeto instance) {
		log.debug("attaching clean CadProjeto instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	   @SuppressWarnings("unchecked")  
	   public List<Object> Query(Object executarComando) {  
	      Query query = (Query) em.createQuery((String) executarComando);  
	      return ((javax.persistence.Query) query).getResultList();  
	      //List<DOG> allDogs = allDogsQuery.getResultList();             
	      //return allDogs;  
	   }  	
	
}